package light.head.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.AuditStatusDAO;
import light.head.dao.oa.ProcessConfigDAO;
import light.head.dao.oa.ResignationDAO;
import light.head.dao.sys.MessageDAO;
import light.head.dao.sys.MessageTemplateDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.AuditStatus;
import light.head.model.ProcessConfig;
import light.head.model.Resignation;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = Url.C_RESIGNATION)
public class ResignationController extends SupporterController{
	
	private ResignationDAO reDAO;
	private UserInfoDAO userDAO;
	private OrganDAO organDAO;
	private ProcessConfigDAO pcDAO;
	private AuditStatusDAO auDAO;
	private MessageDAO msgDAO;
	private MessageTemplateDAO mtDAO;
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.RESIGNATION_INIT);
	}

	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.REASON, Parm.START, Parm.END, Parm.UN, Parm.DELETED);
		map.put(Parm.OC, cu_organCode(request).toString());
		if (reDAO.findByMap(cu_code(request),map, pb) == RESULT.SUCCESS) {
			Resignation re = null;
			for (Object o : pb.getResultList()) {
				re = (Resignation) o;
				if (re.getHead() == false){
					re.setAuditStatusList(auDAO.findByBusinessMc(Sys.B_FIVE, re.getCode()));
				}else{
					re.setAuditStatusList(auDAO.findByBusinessMc(Sys.B_SIX, re.getCode()));
				}
				
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.RESIGNATION_LIST);
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(null == cu_organCode(request)){
			PageBean pb = createPageBean(request);
			request.setAttribute(Parm.PAGE_BEAN, pb);
			return new ModelAndView(Url.RESIGNATION_INIT);
		}
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Resignation res = reDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.RESIGNATION, res);
		}
		return new ModelAndView(Url.RESIGNATION_EDIT);
	}

	@Transactional
	@RequestMapping(value = Url.SAVE)
	public void save(Resignation re, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		Integer oc = cu_organCode(request);
		Integer cu = cu_code(request);
		Integer rc1 =cu_roleCode(request);
		List<ProcessConfig> pcList = new ArrayList<ProcessConfig>();
		if(re.getHead() == false){
			// 检查是否配置审核流程
			 pcList = pcDAO.findByOcBusiness(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_FIVE);
		}else{
			 pcList = pcDAO.findByOcBusiness(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_SIX);
		}
		if (pcList == null || pcList.size() == 0 ) {
			print(response, Msg.EXISTS);
			return;	
		}
		//未审核时修改
		/*if (re.getCode() != null && re.getPass().byteValue() == Sys.B_ONE) {
			Resignation old = reDAO.get(re.getCode());
			result = reDAO.upd(old);
			
		// 审核不通过修改
		} else if(re.getCode() != null && re.getPass().byteValue() == Sys.B_TWO) {
			Resignation old = reDAO.get(re.getCode());
			old.setRemark("再次提交"+ re.getRemark());
			old.setPass(Sys.B_ONE);
			result = reDAO.upd(old);
			
			//重修提交申请
			List<AuditStatus> auList = auDAO.findByBusinessMc(Sys.B_FIVE, re.getCode());
			for (AuditStatus auditStatus : auList) {
				auDAO.del(auditStatus);
			}
			int i = 1;
			for (ProcessConfig pc : pcList) {
				AuditStatus au = new AuditStatus();
				au.setBusiness(Sys.B_FIVE);
				au.setCt(new Date());
				au.setCu(cu);
				au.setOc(oc);
				au.setMc(re.getCode());
				au.setRc(pc.getRc());
				au.setPcc(pc.getCode());
				au.setPc(pc.getPc());
				// 默认第一个审核节点的上一节点审核状态为1通过
				if (null != pc.getPc() && pc.getPc().intValue() == 1) {
					au.setBeforeStatus(Sys.B_ONE);
				}
				if (null != pc.getPc() && pc.getPc().intValue() == 1 && null != rc1 && pc.getRc() == rc1){
					au.setStatus(Sys.B_ONE);
					au.setAt(new Date());
					au.setAu(cu);
					i++;
				}
				if (null != pc.getPc() && pc.getPc().intValue() == 2 && i == 2) {
					au.setBeforeStatus(Sys.B_ONE);
				}
				result = auDAO.add(au);
			}
			// 消息通知审核人
			Integer rc = auDAO.findByBusinessOcMc(Sys.B_FIVE, oc, re.getCode());	// 获取该机构具有审核申请单的角色编号
			if (null != rc) {
				List<UserInfo> userList = userDAO.findByRoleRc(oc, rc);	// 获取有审核角色的所有用户
				List<Integer> rucs = new ArrayList<Integer>();
				for (UserInfo u : userList) {
					rucs.add(u.getCode());
				}
				String content = mtDAO.applyTofirst(old.getCt(), cu_realName(request));
				msgDAO.sendToUsers("【" + cu_realName(request) + "】提交申请待审核通知", content, cu_code(request), cu_organCode(request), rucs);
			}
		} else if(re.getCode() == null) {*/
			re.setCu(cu);
			re.setOc(oc);
			re.setDeleted(false);
			re.setCt(new Date());
			result = reDAO.add(re);
			
			int i = 1;
			for (ProcessConfig pc : pcList) {
				AuditStatus au = new AuditStatus();
				if(re.getHead() == false){
					au.setBusiness(Sys.B_FIVE);
				}else{
					au.setBusiness(Sys.B_SIX);
				}
				au.setCt(new Date());
				au.setCu(cu);
				au.setOc(oc);
				au.setMc(re.getCode());
				au.setRc(pc.getRc());
				au.setPcc(pc.getCode());
				au.setPc(pc.getPc());
				// 默认第一个审核节点的上一节点审核状态为1通过
				if (null != pc.getPc() && pc.getPc().intValue() == 1) {
					au.setBeforeStatus(Sys.B_ONE);
				}
				if (null != pc.getPc() && pc.getPc().intValue() == 1 && null != rc1 && pc.getRc() == rc1){
					au.setStatus(Sys.B_ONE);
					au.setAt(new Date());
					au.setAu(cu);
					i++;
				}
				if (null != pc.getPc() && pc.getPc().intValue() == 2 && i == 2) {
					au.setBeforeStatus(Sys.B_ONE);
				}
				result = auDAO.add(au);
			}
			// 消息通知审核人
			Integer rc;
			if(re.getHead() == false){
				 rc = auDAO.findByBusinessOcMc(Sys.B_FIVE, oc, re.getCode());	// 获取该机构具有审核申请单的角色编号
			} else{
				 rc = auDAO.findByBusinessOcMc(Sys.B_SIX, oc, re.getCode());	// 获取该机构具有审核申请单的角色编号
			}
			if (null != rc) {
				List<UserInfo> userList = userDAO.findByRoleRc(oc, rc);	// 获取有审核角色的所有用户
				List<Integer> rucs = new ArrayList<Integer>();
				for (UserInfo u : userList) {
					rucs.add(u.getCode());
				}
				String content = mtDAO.applyTofirst(re.getCt(), cu_realName(request));
				msgDAO.sendToUsers("【" + cu_realName(request) + "】提交申请待审核通知", content, cu_code(request), cu_organCode(request), rucs);
			}
		//}
		if(result == RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
			return;
		}
	}
	
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Resignation r = reDAO.get(Integer.parseInt(code));
		if (r == null) {
			print(response, Msg.NOT_FIND);
		} else {
			r.setDeleted(true);
			if (reDAO.upd(r) == RESULT.SUCCESS) {
				print(response, Msg.SUCCESS);
			} else {
				print(response, Msg.ERROR);
			}
		}
	}
	
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Resignation r = reDAO.get(Integer.parseInt(code));
		if (r == null) {
			print(response, Msg.NOT_FIND);
		} else {
			r.setDeleted(false);
			if (reDAO.upd(r) == RESULT.SUCCESS) {
				print(response, Msg.SUCCESS);
			} else 
				print(response, Msg.ERROR);
		}
	}
	


	@Autowired
	public void setReDAO(ResignationDAO reDAO) {
		this.reDAO = reDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setPcDAO(ProcessConfigDAO pcDAO) {
		this.pcDAO = pcDAO;
	}
	@Autowired
	public void setAuDAO(AuditStatusDAO auDAO) {
		this.auDAO = auDAO;
	}
	@Autowired
	public void setMsgDAO(MessageDAO msgDAO) {
		this.msgDAO = msgDAO;
	}
	@Autowired
	public void setMtDAO(MessageTemplateDAO mtDAO) {
		this.mtDAO = mtDAO;
	}
	
}
